<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from '@/views/bioAuthData/mixins/resize'

const animationDuration = 2000

export default {
  name: 'BarChart',
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '260px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        title: {
          show: true,
          text: '一周内认证统计',
          textStyle: {
            color: 'black'
          }
        },
        tooltip: {
          trigger: 'axis',
          backgroundColor: 'rgba(255,255,255,0.8)', // 设置背景图片 rgba格式
          color: 'black',
          borderWidth: '1', // 边框宽度设置1
          borderColor: 'gray', // 设置边框颜色
          textStyle: {
            color: 'black' // 设置文字颜色
          },
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        legend: {
          data: ['4G', '5G', '星间'],
          right: 2
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data: ['2021/8/26', '2021/8/27', '2021/8/28', '2021/8/29', '2021/8/30', '2021/8/31', '2021/9/1'],
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis: [{
          type: 'value',
          axisTick: {
            show: false
          }
        }],
        series: [{
          name: '4G',
          type: 'bar',
          // stack: 'vistors',
          barWidth: '20%',
          data: [6782, 6783, 7809, 5930, 6289, 6785, 6371],
          animationDuration
        }, {
          name: '5G',
          type: 'bar',
          // stack: 'vistors',
          barWidth: '20%',
          data: [6782, 6783, 7809, 5930, 6289, 6785, 6378],
          animationDuration
        }, {
          name: '星间',
          type: 'bar',
          // stack: 'vistors', 图形在数值上叠加
          barWidth: '20%',
          data: [3223, 3214, 3124, 3124, 3723, 3479, 3890],
          animationDuration
        }]
      })
    }
  }
}
</script>
